package com.demo.webmagic.pipeline;

import java.util.List;

import com.demo.common.constant.ConstantDtoFlight;
import com.demo.common.model.Proxyip;
import com.demo.text.TestProxyIp;
import com.demo.webmagic.dto.DtoProxyIp;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

public class PipelineAnxiaomo implements Pipeline {

	@Override
	public void process(ResultItems resultItems, Task task) {
		DtoProxyIp dtoProxyIp = resultItems.get("result");
		if(dtoProxyIp!=null && dtoProxyIp.getCode() == ConstantDtoFlight.code_success) {
			List<Proxyip> proxyipList = dtoProxyIp.getProxyIpList();
			//验证ip
			TestProxyIp testProxyIp = new TestProxyIp(proxyipList);
			try {
				proxyipList = testProxyIp.getValidIp();
				for (Proxyip proxyip : proxyipList) {
					//根据ip去数据库中查，如果有则更新时间，如果没有则添加
					Proxyip _proxyip = Proxyip.dao.findFirst("SELECT * FROM proxyip WHERE ipStr = ?", proxyip.getIpStr());
					if(_proxyip == null) {
						proxyip.save();
					} else {
						_proxyip.setValiDate(proxyip.getValiDate());
						_proxyip.update();
					}
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} else {
			throw new RuntimeException("访问异常");
		}
	}

}
